//package com.aaa.order.controller;
//
//import com.aaa.entity.Order;
//import com.aaa.entity.Product;
//import com.aaa.order.service.IOrderService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.client.ServiceInstance;
//import org.springframework.cloud.client.discovery.DiscoveryClient;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.client.RestTemplate;
//
//import java.util.List;
//import java.util.Random;
//
///**
// * @program: qy156-shop-parent
// * @description:
// * @author: 闫克起2
// * @create: 2022-11-17 16:33
// **/
//@RestController
//@RequestMapping("/order")
//public class OrderController01 {
//
//
//    //必须创建并交于spring容器管理。这样才能被注入到相应的类属性上
//    @Autowired
//    private RestTemplate restTemplate;
//
//    @Autowired
//    private IOrderService orderService;
//
//    @Autowired
//    private DiscoveryClient discoveryClient;
//
//    @GetMapping("/saveOrder")
//    public String saveOrder(Long pid,Integer num){
//        Order order=new Order();
//        //用户得信息可以从登录后获取--Session  redis  jwt
//        order.setUid(1);
//        order.setUsername("张成");
//        //为订单对象设置商品得信息
//        order.setPid(pid);
//        //在nacos中封装了一个类DiscoveryClient，该类可以获取注册中心中指定的清单列表。
//        //获取指定的实例
//        List<ServiceInstance> instances = discoveryClient.getInstances("shop-product");
//
//        //随机产生一个下标--0~size
//        int index = new Random().nextInt(instances.size());
//        ServiceInstance serviceInstance = instances.get(index);
////        String path = serviceInstance.getHost().toString();
////        System.out.println(path+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
////        Integer port = serviceInstance.getPort();
////        System.out.println(port+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
//
//        String uri = serviceInstance.getUri().toString();
////        System.out.println(uri+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
//        Product product = restTemplate.getForObject(uri+"/product/getById/"+pid, Product.class);
//        order.setPname(product.getPname());
//        order.setPprice(product.getPprice());
//        order.setNumber(num);
//
//        orderService.save(order);
//
//        return "下单成功";
//    }
//}
